package com.myapp.db.entity;

import java.nio.ByteBuffer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.atomic.*;

import com.myapp.data.define.BaseBean;
import com.myapp.db.config.ann.TableDefine;
import com.myapp.db.config.define.UpdateData;
import com.myapp.db.dao.manual.RoleDao;

/**
 * 文件名：RoleEntity.java
 * <p>
 * 功能：
 * <p>
 * 作者：Builded by AppTools.
 */
@TableDefine(name = "Role", dao = RoleDao.class)
public class RoleEntity extends BaseBean {
	// 用户id
	private AtomicLong userid = new AtomicLong(0);
	
	// 角色名称
	private String name = null;
	
	// 充值次数
	private AtomicInteger chargecount = new AtomicInteger(0);
	
	// 充值总额
	private AtomicLong totalmoeny = new AtomicLong(0);
	
	// vip等级
	private AtomicLong viplv = new AtomicLong(0);
	
	// 金币
	private AtomicLong gold = new AtomicLong(0);
	
	// 银币
	private AtomicLong silver = new AtomicLong(0);
	
	// 功勋
	private AtomicLong exploit = new AtomicLong(0);
	
	// 级别
	private AtomicInteger lv = new AtomicInteger(0);
	
	// 经验
	private AtomicLong exp = new AtomicLong(0);
	
	// 当前地图id
	private AtomicInteger mapid = new AtomicInteger(0);
	
	// 当前怪物波数
	private AtomicInteger waveid = new AtomicInteger(0);
	
	// 当前军令数
	private AtomicInteger writ = new AtomicInteger(0);
	
	// 此条记录最后修改时间
	private AtomicLong updatetime = new AtomicLong(0);
	

	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public long getUserid() {
		return this.userid.get();
	}

	/**
	 * 原子增加用户id
	 * 
	 * @param userid 要加的值（正数）
	 */
	public void addUserid(long userid) {
		UpdateData data = new UpdateData(this.userid.get(), userid);
		this.getOldData().put("userid", data);
		this.userid.addAndGet(userid);
	}

	/**
	 * 原子减少用户id
	 * 
	 * @param userid 要减的值（正数）
	 */
	public void minusUserid(long userid) {
		UpdateData data = new UpdateData(this.userid.get(), userid);
		this.getOldData().put("userid", data);
		this.userid.addAndGet(userid*-1);
	}
	
	/**
	 * 直接设置userid的值
	 * 
	 * @param userid 要设置的新值
	 */
	public void setUserid(long userid){
		UpdateData data = new UpdateData(this.userid.get(), userid);
		this.getOldData().put("userid", data);
		this.userid.set(userid);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * 直接设置name的值
	 * 
	 * @param name 要设置的新值
	 */
	public void setName(String name) {
		if(this.name == null){
			UpdateData data = new UpdateData("", name);
			this.getOldData().put("name", data);
		}else{
			UpdateData data = new UpdateData(this.name, name);
			this.getOldData().put("name", data);
		}
		this.name = name;
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public int getChargecount() {
		return chargecount.get();
	}

	/**
	 * 原子增加充值次数
	 * 
	 * @param chargecount 要加的值（正数）
	 */
	public void addChargecount(int chargecount) {
		UpdateData data = new UpdateData(this.chargecount.get(), chargecount);
		this.getOldData().put("chargecount", data);
		this.chargecount.addAndGet(chargecount);
	}
	
	/**
	 * 原子减少充值次数
	 * 
	 * @param chargecount 要减的值（正数）
	 */
	public void minusChargecount(int chargecount) {
		UpdateData data = new UpdateData(this.chargecount.get(), chargecount);
		this.getOldData().put("chargecount", data);
		this.chargecount.addAndGet(chargecount*-1);
	}
	
	/**
	 * 直接设置chargecount的值
	 * 
	 * @param chargecount 要设置的新值
	 */
	public void setChargecount(int chargecount){
		UpdateData data = new UpdateData(this.chargecount.get(), chargecount);
		this.getOldData().put("chargecount", data);
		this.chargecount.set(chargecount);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public long getTotalmoeny() {
		return this.totalmoeny.get();
	}

	/**
	 * 原子增加充值总额
	 * 
	 * @param totalmoeny 要加的值（正数）
	 */
	public void addTotalmoeny(long totalmoeny) {
		UpdateData data = new UpdateData(this.totalmoeny.get(), totalmoeny);
		this.getOldData().put("totalmoeny", data);
		this.totalmoeny.addAndGet(totalmoeny);
	}

	/**
	 * 原子减少充值总额
	 * 
	 * @param totalmoeny 要减的值（正数）
	 */
	public void minusTotalmoeny(long totalmoeny) {
		UpdateData data = new UpdateData(this.totalmoeny.get(), totalmoeny);
		this.getOldData().put("totalmoeny", data);
		this.totalmoeny.addAndGet(totalmoeny*-1);
	}
	
	/**
	 * 直接设置totalmoeny的值
	 * 
	 * @param totalmoeny 要设置的新值
	 */
	public void setTotalmoeny(long totalmoeny){
		UpdateData data = new UpdateData(this.totalmoeny.get(), totalmoeny);
		this.getOldData().put("totalmoeny", data);
		this.totalmoeny.set(totalmoeny);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public long getViplv() {
		return this.viplv.get();
	}

	/**
	 * 原子增加vip等级
	 * 
	 * @param viplv 要加的值（正数）
	 */
	public void addViplv(long viplv) {
		UpdateData data = new UpdateData(this.viplv.get(), viplv);
		this.getOldData().put("viplv", data);
		this.viplv.addAndGet(viplv);
	}

	/**
	 * 原子减少vip等级
	 * 
	 * @param viplv 要减的值（正数）
	 */
	public void minusViplv(long viplv) {
		UpdateData data = new UpdateData(this.viplv.get(), viplv);
		this.getOldData().put("viplv", data);
		this.viplv.addAndGet(viplv*-1);
	}
	
	/**
	 * 直接设置viplv的值
	 * 
	 * @param viplv 要设置的新值
	 */
	public void setViplv(long viplv){
		UpdateData data = new UpdateData(this.viplv.get(), viplv);
		this.getOldData().put("viplv", data);
		this.viplv.set(viplv);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public long getGold() {
		return this.gold.get();
	}

	/**
	 * 原子增加金币
	 * 
	 * @param gold 要加的值（正数）
	 */
	public void addGold(long gold) {
		UpdateData data = new UpdateData(this.gold.get(), gold);
		this.getOldData().put("gold", data);
		this.gold.addAndGet(gold);
	}

	/**
	 * 原子减少金币
	 * 
	 * @param gold 要减的值（正数）
	 */
	public void minusGold(long gold) {
		UpdateData data = new UpdateData(this.gold.get(), gold);
		this.getOldData().put("gold", data);
		this.gold.addAndGet(gold*-1);
	}
	
	/**
	 * 直接设置gold的值
	 * 
	 * @param gold 要设置的新值
	 */
	public void setGold(long gold){
		UpdateData data = new UpdateData(this.gold.get(), gold);
		this.getOldData().put("gold", data);
		this.gold.set(gold);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public long getSilver() {
		return this.silver.get();
	}

	/**
	 * 原子增加银币
	 * 
	 * @param silver 要加的值（正数）
	 */
	public void addSilver(long silver) {
		UpdateData data = new UpdateData(this.silver.get(), silver);
		this.getOldData().put("silver", data);
		this.silver.addAndGet(silver);
	}

	/**
	 * 原子减少银币
	 * 
	 * @param silver 要减的值（正数）
	 */
	public void minusSilver(long silver) {
		UpdateData data = new UpdateData(this.silver.get(), silver);
		this.getOldData().put("silver", data);
		this.silver.addAndGet(silver*-1);
	}
	
	/**
	 * 直接设置silver的值
	 * 
	 * @param silver 要设置的新值
	 */
	public void setSilver(long silver){
		UpdateData data = new UpdateData(this.silver.get(), silver);
		this.getOldData().put("silver", data);
		this.silver.set(silver);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public long getExploit() {
		return this.exploit.get();
	}

	/**
	 * 原子增加功勋
	 * 
	 * @param exploit 要加的值（正数）
	 */
	public void addExploit(long exploit) {
		UpdateData data = new UpdateData(this.exploit.get(), exploit);
		this.getOldData().put("exploit", data);
		this.exploit.addAndGet(exploit);
	}

	/**
	 * 原子减少功勋
	 * 
	 * @param exploit 要减的值（正数）
	 */
	public void minusExploit(long exploit) {
		UpdateData data = new UpdateData(this.exploit.get(), exploit);
		this.getOldData().put("exploit", data);
		this.exploit.addAndGet(exploit*-1);
	}
	
	/**
	 * 直接设置exploit的值
	 * 
	 * @param exploit 要设置的新值
	 */
	public void setExploit(long exploit){
		UpdateData data = new UpdateData(this.exploit.get(), exploit);
		this.getOldData().put("exploit", data);
		this.exploit.set(exploit);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public int getLv() {
		return lv.get();
	}

	/**
	 * 原子增加级别
	 * 
	 * @param lv 要加的值（正数）
	 */
	public void addLv(int lv) {
		UpdateData data = new UpdateData(this.lv.get(), lv);
		this.getOldData().put("lv", data);
		this.lv.addAndGet(lv);
	}
	
	/**
	 * 原子减少级别
	 * 
	 * @param lv 要减的值（正数）
	 */
	public void minusLv(int lv) {
		UpdateData data = new UpdateData(this.lv.get(), lv);
		this.getOldData().put("lv", data);
		this.lv.addAndGet(lv*-1);
	}
	
	/**
	 * 直接设置lv的值
	 * 
	 * @param lv 要设置的新值
	 */
	public void setLv(int lv){
		UpdateData data = new UpdateData(this.lv.get(), lv);
		this.getOldData().put("lv", data);
		this.lv.set(lv);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public long getExp() {
		return this.exp.get();
	}

	/**
	 * 原子增加经验
	 * 
	 * @param exp 要加的值（正数）
	 */
	public void addExp(long exp) {
		UpdateData data = new UpdateData(this.exp.get(), exp);
		this.getOldData().put("exp", data);
		this.exp.addAndGet(exp);
	}

	/**
	 * 原子减少经验
	 * 
	 * @param exp 要减的值（正数）
	 */
	public void minusExp(long exp) {
		UpdateData data = new UpdateData(this.exp.get(), exp);
		this.getOldData().put("exp", data);
		this.exp.addAndGet(exp*-1);
	}
	
	/**
	 * 直接设置exp的值
	 * 
	 * @param exp 要设置的新值
	 */
	public void setExp(long exp){
		UpdateData data = new UpdateData(this.exp.get(), exp);
		this.getOldData().put("exp", data);
		this.exp.set(exp);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public int getMapid() {
		return mapid.get();
	}

	/**
	 * 原子增加当前地图id
	 * 
	 * @param mapid 要加的值（正数）
	 */
	public void addMapid(int mapid) {
		UpdateData data = new UpdateData(this.mapid.get(), mapid);
		this.getOldData().put("mapid", data);
		this.mapid.addAndGet(mapid);
	}
	
	/**
	 * 原子减少当前地图id
	 * 
	 * @param mapid 要减的值（正数）
	 */
	public void minusMapid(int mapid) {
		UpdateData data = new UpdateData(this.mapid.get(), mapid);
		this.getOldData().put("mapid", data);
		this.mapid.addAndGet(mapid*-1);
	}
	
	/**
	 * 直接设置mapid的值
	 * 
	 * @param mapid 要设置的新值
	 */
	public void setMapid(int mapid){
		UpdateData data = new UpdateData(this.mapid.get(), mapid);
		this.getOldData().put("mapid", data);
		this.mapid.set(mapid);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public int getWaveid() {
		return waveid.get();
	}

	/**
	 * 原子增加当前怪物波数
	 * 
	 * @param waveid 要加的值（正数）
	 */
	public void addWaveid(int waveid) {
		UpdateData data = new UpdateData(this.waveid.get(), waveid);
		this.getOldData().put("waveid", data);
		this.waveid.addAndGet(waveid);
	}
	
	/**
	 * 原子减少当前怪物波数
	 * 
	 * @param waveid 要减的值（正数）
	 */
	public void minusWaveid(int waveid) {
		UpdateData data = new UpdateData(this.waveid.get(), waveid);
		this.getOldData().put("waveid", data);
		this.waveid.addAndGet(waveid*-1);
	}
	
	/**
	 * 直接设置waveid的值
	 * 
	 * @param waveid 要设置的新值
	 */
	public void setWaveid(int waveid){
		UpdateData data = new UpdateData(this.waveid.get(), waveid);
		this.getOldData().put("waveid", data);
		this.waveid.set(waveid);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public int getWrit() {
		return writ.get();
	}

	/**
	 * 原子增加当前军令数
	 * 
	 * @param writ 要加的值（正数）
	 */
	public void addWrit(int writ) {
		UpdateData data = new UpdateData(this.writ.get(), writ);
		this.getOldData().put("writ", data);
		this.writ.addAndGet(writ);
	}
	
	/**
	 * 原子减少当前军令数
	 * 
	 * @param writ 要减的值（正数）
	 */
	public void minusWrit(int writ) {
		UpdateData data = new UpdateData(this.writ.get(), writ);
		this.getOldData().put("writ", data);
		this.writ.addAndGet(writ*-1);
	}
	
	/**
	 * 直接设置writ的值
	 * 
	 * @param writ 要设置的新值
	 */
	public void setWrit(int writ){
		UpdateData data = new UpdateData(this.writ.get(), writ);
		this.getOldData().put("writ", data);
		this.writ.set(writ);
	}
	
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public long getUpdatetime() {
		return this.updatetime.get();
	}

	/**
	 * 原子增加此条记录最后修改时间
	 * 
	 * @param updatetime 要加的值（正数）
	 */
	public void addUpdatetime(long updatetime) {
		UpdateData data = new UpdateData(this.updatetime.get(), updatetime);
		this.getOldData().put("updatetime", data);
		this.updatetime.addAndGet(updatetime);
	}

	/**
	 * 原子减少此条记录最后修改时间
	 * 
	 * @param updatetime 要减的值（正数）
	 */
	public void minusUpdatetime(long updatetime) {
		UpdateData data = new UpdateData(this.updatetime.get(), updatetime);
		this.getOldData().put("updatetime", data);
		this.updatetime.addAndGet(updatetime*-1);
	}
	
	/**
	 * 直接设置updatetime的值
	 * 
	 * @param updatetime 要设置的新值
	 */
	public void setUpdatetime(long updatetime){
		UpdateData data = new UpdateData(this.updatetime.get(), updatetime);
		this.getOldData().put("updatetime", data);
		this.updatetime.set(updatetime);
	}
	

	@Override
	public void parseEntity(ResultSet rest) throws SQLException {
		// id
		this.setId(rest.getLong(1));
		// 用户id
		this.userid.set(rest.getLong(2));
		// 角色名称
		String tmp = rest.getString(3);
		if (!"null".equals(tmp)) {
			this.name = tmp;
		}
		// 充值次数
		this.chargecount.set(rest.getInt(4));
		// 充值总额
		this.totalmoeny.set(rest.getLong(5));
		// vip等级
		this.viplv.set(rest.getLong(6));
		// 金币
		this.gold.set(rest.getLong(7));
		// 银币
		this.silver.set(rest.getLong(8));
		// 功勋
		this.exploit.set(rest.getLong(9));
		// 级别
		this.lv.set(rest.getInt(10));
		// 经验
		this.exp.set(rest.getLong(11));
		// 当前地图id
		this.mapid.set(rest.getInt(12));
		// 当前怪物波数
		this.waveid.set(rest.getInt(13));
		// 当前军令数
		this.writ.set(rest.getInt(14));
		// 此条记录最后修改时间
		this.updatetime.set(rest.getLong(15));
	}
	
	
	/**
	 * 将本对象变化的数据序列化
	 * 
	 * @return 返回序列化后的数据
	 */
	@Override
	public byte[] serialization() {
	ByteBuffer data = ByteBuffer.allocate(1024);
		if (this.getOldData().containsKey("userid")) {
			data = this.checkCapacity(data, 8);
			data.put((byte) 0);
			data.putLong(userid.get());
		}
		if (this.getOldData().containsKey("name")) {
			byte[] tmp=name.getBytes();
			data = this.checkCapacity(data, tmp.length);
			data.put((byte) 1);
			data.putInt(tmp.length);
			data.put(tmp);
		}
		if (this.getOldData().containsKey("chargecount")) {
			data = this.checkCapacity(data, 4);
			data.put((byte) 2);
			data.putInt(chargecount.get());
		}
		if (this.getOldData().containsKey("totalmoeny")) {
			data = this.checkCapacity(data, 8);
			data.put((byte) 3);
			data.putLong(totalmoeny.get());
		}
		if (this.getOldData().containsKey("viplv")) {
			data = this.checkCapacity(data, 8);
			data.put((byte) 4);
			data.putLong(viplv.get());
		}
		if (this.getOldData().containsKey("gold")) {
			data = this.checkCapacity(data, 8);
			data.put((byte) 5);
			data.putLong(gold.get());
		}
		if (this.getOldData().containsKey("silver")) {
			data = this.checkCapacity(data, 8);
			data.put((byte) 6);
			data.putLong(silver.get());
		}
		if (this.getOldData().containsKey("exploit")) {
			data = this.checkCapacity(data, 8);
			data.put((byte) 7);
			data.putLong(exploit.get());
		}
		if (this.getOldData().containsKey("lv")) {
			data = this.checkCapacity(data, 4);
			data.put((byte) 8);
			data.putInt(lv.get());
		}
		if (this.getOldData().containsKey("exp")) {
			data = this.checkCapacity(data, 8);
			data.put((byte) 9);
			data.putLong(exp.get());
		}
		if (this.getOldData().containsKey("mapid")) {
			data = this.checkCapacity(data, 4);
			data.put((byte) 10);
			data.putInt(mapid.get());
		}
		if (this.getOldData().containsKey("waveid")) {
			data = this.checkCapacity(data, 4);
			data.put((byte) 11);
			data.putInt(waveid.get());
		}
		if (this.getOldData().containsKey("writ")) {
			data = this.checkCapacity(data, 4);
			data.put((byte) 12);
			data.putInt(writ.get());
		}
		if (this.getOldData().containsKey("updatetime")) {
			data = this.checkCapacity(data, 8);
			data.put((byte) 13);
			data.putLong(updatetime.get());
		}
		data.flip();
		return data.array();
	}
	
	
	/**
	 * 反序列化
	 * 
	 * @param data 要反序列化的数据
	 * 
	 * @return 返回序列化出来的对象
	 */
	@SuppressWarnings("unchecked")
	@Override
	public RoleEntity unSerialization(byte[] data) {
		RoleEntity roleEntity = new RoleEntity();
		if (data.length > 0) {

			ByteBuffer buffer = ByteBuffer.wrap(data);
			buffer.flip();

			byte type = 0;
			while (buffer.position() < buffer.limit()) {
				type = buffer.get();
			if (type ==  0) {
				this.setUserid(buffer.getLong());
			}
			
			if (type ==  1) {
				int length=buffer.getInt();
				byte[] tmp=new byte[length];
				buffer.get(tmp);
				this.setName(new String(tmp));
			}
			
			if (type ==  2) {
				this.setChargecount(buffer.getInt());
			}
			
			if (type ==  3) {
				this.setTotalmoeny(buffer.getLong());
			}
			
			if (type ==  4) {
				this.setViplv(buffer.getLong());
			}
			
			if (type ==  5) {
				this.setGold(buffer.getLong());
			}
			
			if (type ==  6) {
				this.setSilver(buffer.getLong());
			}
			
			if (type ==  7) {
				this.setExploit(buffer.getLong());
			}
			
			if (type ==  8) {
				this.setLv(buffer.getInt());
			}
			
			if (type ==  9) {
				this.setExp(buffer.getLong());
			}
			
			if (type ==  10) {
				this.setMapid(buffer.getInt());
			}
			
			if (type ==  11) {
				this.setWaveid(buffer.getInt());
			}
			
			if (type ==  12) {
				this.setWrit(buffer.getInt());
			}
			
			if (type ==  13) {
				this.setUpdatetime(buffer.getLong());
			}
			
			}
		}

		return roleEntity;
	}
}
